package com.tm.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.tm.constants.AliMsgConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @author huangp
 * @create 2023-04-17 18:46
 */
@Component
@Slf4j
public class AliyunMessage {


    @Value("${aliyun.msg.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.msg.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.msg.signName}")
    private String signName;

    @Value("${aliyun.msg.endpoint}")
    private String endpoint;

    public Boolean sendMsg(String phone ,String code){
        return sendMsg(phone,code,signName,AliMsgConstants.TEMPLAET_CODE_TEST);
    }

    private Boolean sendMsg(String phone, String code,String templateCode) {
        return sendMsg(phone,code,signName,templateCode);
    }

    private Boolean sendMsg(String phone, String code, String signName, String templateCode) {
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        Client client = createClient(accessKeyId,accessKeySecret);
        if(client == null){
            return Boolean.FALSE;
        }
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            SendSmsResponseBody sendSmsResponseBody = sendSmsResponse.getBody();
            if(sendSmsResponseBody.getCode().equals("OK")){
                log.info("验证码发送成功,手机号为[{}]",phone);
                return Boolean.TRUE;
            }
            System.out.println(sendSmsResponse);
        } catch (TeaException error) {
            // 如有需要，请打印 error
           log.error(error.message);
        } catch (Exception _error) {
            // 如有需要，请打印 error
            log.error(_error.getMessage());
        }
        return Boolean.FALSE;
    }


    public Client createClient(String accessKeyId, String accessKeySecret){
        Config config = new  Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        try {
            return new Client(config);
        } catch (Exception e) {
           log.error(e.getMessage());
           return null;
        }
    }


}
